package com.wg.springbootencoding.servletconfig;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.servlet.Filter;

/**
 * @Description:
 * @Author WuGang
 * @Date 2021/10/21 19:02
 * @Version 1.0
 */
@Configuration
public class ServletConfig {
    @Bean
    public FilterRegistrationBean registrationBean(){
        //2.字符编码过滤器对象
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
//        设置是否指定使用字符编码
        characterEncodingFilter.setForceEncoding(true);
//        设置字符集
        characterEncodingFilter.setEncoding("utf-8");

        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
//        1.设置字符编码过滤器
        filterFilterRegistrationBean.setFilter(characterEncodingFilter);
//        3.设置过滤路径
        filterFilterRegistrationBean.addUrlPatterns("/*");
        return filterFilterRegistrationBean;
    }
}
